﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

using System.Data.SqlClient;


namespace baitaptuan5
{
    public partial class BaoCaoTon : Form
    {
        public BaoCaoTon()
        {
            InitializeComponent();
        }
        PhieuNhapBUS pnbus = new PhieuNhapBUS();
        DataTable dtNhapHangThan;
        DataTable dtBanTrongThang;
        string m;

        private void BaoCaoTon_Load(object sender, EventArgs e)
        {
            
            this.comboBox1.SelectedItem = "4";
            m = this.comboBox1.SelectedItem.ToString();

            dataGridViewTon.DataSource = dtBanTrongThang;
            dtNhapHangThan = pnbus.LayTableTheoSachThang(m);
            dtBanTrongThang = pnbus.LaySoLuongSachBanTrong1Thang(m);

            this.dataGridViewTon.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //this.dataGridViewTon.Rows[0].Selected = false;
            this.dataGridViewTon.DefaultCellStyle.SelectionBackColor = Color.Red;

        }
        private void dispose()
        {
            base.Dispose();
            components.Dispose();
        }

        private void thoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dispose();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //DataTable table_BaoCaoTon_nguoc = new DataTable();
            //foreach (DataGridViewColumn col in this.dtgBaoCaoTonMoi.Columns)
            //{
            //    table_BaoCaoTon_nguoc.Columns.Add(col.Name);
            //}

            //foreach (DataGridViewRow rows in this.dtgBaoCaoTonMoi.Rows)
            //{
            //    DataRow row = table_BaoCaoTon_nguoc.NewRow();
            //    row[0] = rows.Cells[0].Value;
            //    row[1] = rows.Cells[1].Value;
            //    row[2] = rows.Cells[2].Value;
            //    row[3] = rows.Cells[3].Value;
            //    row[4] = rows.Cells[4].Value;
            //    row[5] = rows.Cells[5].Value;
            //    row[6] = rows.Cells[6].Value;
            //    table_BaoCaoTon_nguoc.Rows.Add(row);
            //    //lUu du lieu tu datagridview vao data table  END
            //}
            BaoCaoTon_Load(sender, e);
        }

        private void dataGridViewTon_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            BaoCaoTon_Load(sender, e);
        }

        private void dataGridViewTon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.RowIndex < 0)
                return;
            else
            this.dataGridViewTon.SelectedRows[0].DefaultCellStyle.SelectionBackColor = Color.Red;
            
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            this.dataGridViewTon.DefaultCellStyle.SelectionBackColor = Color.Red;

            m = this.comboBox1.SelectedItem.ToString();
            
            dtNhapHangThan = pnbus.LayTableTheoSachThang(m);
            dtBanTrongThang = pnbus.LaySoLuongSachBanTrong1Thang(m);

            dataGridViewTon.DataSource = dtBanTrongThang;

            int tien;
            int r = 0;

            int sumrowban = dtBanTrongThang.Rows.Count;
            int sumrownhap = dtNhapHangThan.Rows.Count;

            // giai quyet 1 chieu bán nhiều hơn nhập  Xu

            if (sumrowban >= sumrownhap)
            {
                foreach (DataRow rowBan in dtBanTrongThang.Rows)
                {
                    foreach (DataRow rowNhap in dtNhapHangThan.Rows)
                    {
                        if (rowBan["masach"].ToString() == rowNhap["masach"].ToString())
                        {
                            tien = int.Parse(rowBan["banhangthang"].ToString()) - int.Parse(rowNhap["nhaphangthang"].ToString());

                            if (tien < -1)
                                tien = tien * (-1);

                            this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value = tien.ToString();
                            this.dataGridViewTon.Rows[r].Cells["tondau"].Value = int.Parse(rowBan["soluong"].ToString()) - tien;
                        }
                        this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value = rowBan["banhangthang"];
                        this.dataGridViewTon.Rows[r].Cells["tondau"].Value = int.Parse(rowBan["soluong"].ToString()) - int.Parse(this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value.ToString());
                    }
                    r++;
                }
            }
            else
            {
                foreach (DataRow rowNhap in dtNhapHangThan.Rows)
                {
                    foreach (DataRow rowBan in dtBanTrongThang.Rows)
                    {
                        if (rowBan["masach"].ToString() == rowNhap["masach"].ToString())
                        {
                            tien = int.Parse(rowBan["banhangthang"].ToString()) - int.Parse(rowNhap["nhaphangthang"].ToString());

                            if (tien < -1)
                                tien = tien * (-1);

                            this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value = tien.ToString();
                            this.dataGridViewTon.Rows[r].Cells["tondau"].Value = int.Parse(rowBan["soluong"].ToString()) - tien;
                        }
                        this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value = rowBan["nhaphangthang"];
                        this.dataGridViewTon.Rows[r].Cells["tondau"].Value = int.Parse(rowBan["soluong"].ToString()) - int.Parse(this.dataGridViewTon.Rows[r].Cells["phatsinh"].Value.ToString());
                    }
                    r++;
                }

            }
        }

       

      

    }
}
